package com.eightbears.beboy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.entity.OrderEntity;
import com.makeramen.roundedimageview.RoundedImageView;

import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ALREADY_COMMENT;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_APPLY_REFOUND;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_END_DATE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_REFOUND_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_UNUSE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_USE;

public class OrderListAdapter extends BaseQuickAdapter<OrderEntity.OrderInfo, BaseViewHolder> {

    private String btnText;
    private String btnTag;
    private ImageView statusImg;
    private TextView eventBtn;

    public OrderListAdapter() {
        super(R.layout.item_order, null);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, OrderEntity.OrderInfo orderInfo) {

        statusImg = baseViewHolder.getView(R.id.iv_shopbook_use);
        eventBtn = baseViewHolder.getView(R.id.tv_shopbook_mealbug);
        replaceView();

        if(orderInfo.getCoupon() == null) {
            CommonUtils.loadImageView(mContext, "", R.mipmap.icon_default_shop,
                    (RoundedImageView) baseViewHolder.getView(R.id.iv_shopbook_image));
            baseViewHolder.setText(R.id.tv_shopbook_mealname, "店铺异常")
                    .setText(R.id.tv_shopbook_mealprice, "")
                    .setText(R.id.tv_shopbook_mealoldprice, "")
                    .setText(R.id.tv_shopbook_mealbug, btnText)
                    .setText(R.id.tv_date, "")
                    .setText(R.id.iv_shopbook_left, "");

        } else {
            CommonUtils.loadImageView(mContext, orderInfo.getCoupon().getCover(), R.mipmap.icon_default_shop,
                    (RoundedImageView) baseViewHolder.getView(R.id.iv_shopbook_image));

            if (!(TextUtils.isEmpty(orderInfo.getStatus()))) {
                if (orderInfo.getStatus().equals(STATUS_UNUSE)) {//未使用
                    btnText = "使用";
                    btnTag = STATUS_UNUSE;
                    showUse();
                } else if (orderInfo.getStatus().equals(STATUS_USE)) {//已使用
                    btnText = "评论";
                    btnTag = STATUS_USE;
                    showStatusUse();
                } else if (orderInfo.getStatus().equals(STATUS_END_DATE)) {//已过期
                    btnTag = STATUS_END_DATE;
                    showStatusDateEnd();
                } else if (orderInfo.getStatus().equals(STATUS_APPLY_REFOUND)) {
                    btnText = "退款中";
                    btnTag = STATUS_APPLY_REFOUND;
                    showRefund();
                } else if (orderInfo.getStatus().equals(STATUS_REFOUND_SUCCESS)) {
                    btnText = "已退款";
                    btnTag = STATUS_REFOUND_SUCCESS;
                    showRefundSuccess();
                } else if(orderInfo.getStatus().equals(STATUS_ALREADY_COMMENT)) {
                    btnText = "已评论";
                    btnTag = STATUS_ALREADY_COMMENT;
                    alreadyComment();
                }
            }

            String startDate = CommonUtils.TimeStamp2Date(orderInfo.getCoupon().getStart_at() + "", "yyyy-MM-dd");
            String endDate = CommonUtils.TimeStamp2Date(orderInfo.getCoupon().getEnd_at() + "", "yyyy-MM-dd");
            String dateString = String.format(mContext.getResources().getString(R.string.text_valid_date), startDate, endDate);
//        String Result = Double.valueOf(orderInfo.getCoupon().getDiscount_price());
            baseViewHolder.setText(R.id.tv_shopbook_mealname, orderInfo.getCoupon().getTitle())
                    .setText(R.id.tv_shopbook_mealprice, "￥" + orderInfo.getOrder().getReceipt_amount())
                    .setText(R.id.tv_shopbook_mealoldprice, "原价:￥" + orderInfo.getCoupon().getPrice())
                    .setText(R.id.tv_shopbook_mealbug, btnText)
                    .setText(R.id.tv_date, dateString)
                    .setText(R.id.iv_shopbook_left, orderInfo.getShop().getTitle())
                    .setTag(R.id.tv_shopbook_mealbug, btnTag)
                    .addOnClickListener(R.id.tv_shopbook_mealbug)
                    .addOnClickListener(R.id.shop_info_meal);
        }
    }

    private void showUse() {
        eventBtn.setVisibility(View.VISIBLE);
        eventBtn.setBackgroundResource(R.drawable.background_circle_bead_blue_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));
    }

    /**
     * 显示已使用
     */
    private void showStatusUse() {
        eventBtn.setVisibility(View.VISIBLE);
        eventBtn.setBackgroundResource(R.drawable.background_circle_bead_blue_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));
        statusImg.setVisibility(View.VISIBLE);
        statusImg.setImageResource(R.mipmap.icon_shiyong);
    }

    /**
     * 显示已过期
     */
    private void showStatusDateEnd() {
        statusImg.setVisibility(View.VISIBLE);
        statusImg.setImageResource(R.mipmap.icon_shixiao);
    }

    /**
     * 显示退款中
     */
    private void showRefund() {
        eventBtn.setBackgroundResource(R.drawable.bg_gray_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));
        eventBtn.setVisibility(View.VISIBLE);
    }

    /**
     * 显示已退款
     */
    private void showRefundSuccess() {
        eventBtn.setBackgroundResource(R.color.text_color_fff);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        eventBtn.setVisibility(View.VISIBLE);
        statusImg.setVisibility(View.VISIBLE);
        statusImg.setImageResource(R.mipmap.icon_shixiao);
    }

    /**
     * 已评论
     */
    private void alreadyComment() {
        eventBtn.setBackgroundResource(R.color.text_color_fff);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_price_red_color));
        eventBtn.setVisibility(View.VISIBLE);
    }

    private void replaceView() {
        statusImg.setVisibility(View.GONE);
        eventBtn.setVisibility(View.GONE);
    }
}
